
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package picturemouse;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

/**
 *
 * @author Duncan
 */
public class Account {
    
    private String customerName;
    private String password;
    private String cardNumber;

    public Account(String name, String pwd, String ccNo) throws InvalidCardNumberException, InvalidPasswordException, AccountAlreadyExistsException {
    	
    	if (validName(name)) {
    		customerName = name;
    	}
    	else {
    		throw new AccountAlreadyExistsException();
    	}
    	    	
    	if (validPassword(pwd)) {
    		password = pwd;
    	}
    	else {
    		throw new InvalidPasswordException();
    	}
    	
    	if (validateCreditCard(ccNo)) {
    		cardNumber = ccNo;
    	}
    	else {
    		throw new InvalidCardNumberException();
    	}

    }
    
    /**
     *
     */
    private Boolean validateCreditCard(String num) {
    	
    	//Ensure number is correct length
    	if (num.length() != 16) {
    		return false;
    	}
    	
    	//Split the string into an array of individual characters
    	char[] chars = num.toCharArray();
    	
    	//Convert to objects (more useful than primitives)
    	Character characters[] = new Character[16];
    	for (int i = 0; i < 16; i++) {
    		characters[i] = new Character(chars[i]);
    	}
    	
    	//Ensure each character is a digit
    	for (Character c : characters) {
    		if (!Character.isDigit(c)) {
    			return false;
    		}
    	}
    	    	
    	//Apply Luhn Algorithm to ensure number is a valid credit card
    	int total = 0;
    	for (int i = 0; i < 16; i++) {
    		if (i == 0 || i % 2 == 0 ) {
    			int doubled = Character.getNumericValue(characters[i])*2;
    			if (doubled < 10) {
    				total = total + doubled;
    			}
    			else {
    				int digit1 = doubled / 10;
    				int digit2 = doubled % 10;
    				total = total + digit1 + digit2;
    			}
    		}
    		else {
    			total = total + Character.getNumericValue(characters[i]);
    		}
    	}
    	
    	if (total % 10 == 0) {
    		return true;
    	}
    	
    	//Return false value if validation fails
    	return false;
    }
    
    /**
    *
    */
    private Boolean validPassword(String pwd) {
    	
    	//Ensure at least 8 characters long
    	if (pwd.length() < 8) {
    		return false;
    	}
    	
    	//Ensure at least one uppercase character
    	Pattern pattern1 = Pattern.compile("[A-Z]");
    	Matcher matcher1 = pattern1.matcher(pwd);
    	if (!matcher1.find()) {
    		return false;
    	}
    	
    	//Ensure at least one digit
    	Pattern pattern2 = Pattern.compile("\\d");
    	Matcher matcher2 = pattern2.matcher(pwd);
    	if (!matcher2.find()) {
    		return false;
    	}
    	
    	//Finally return true if all tests pass
    	return true;
    	
    }
    
    
    private Boolean validName(String name) {
    	
    	try {
    		PictureMouse.getAccountByName(name);
    		return false;
    	}
    	catch (AccountNotFoundException e) {
    		return true;
    	}

    }
    
    
    /**
     * @return the customerName
     */
    public String getCustomerName() {
        return customerName;
    }

    /**
     * @param customerName the customerName to set
     */
    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the cardNumber
     */
    public String getCardNumber() {
        return cardNumber;
    }

    /**
     * @param cardNumber the cardNumber to set
     */
    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }
    
    public void changePassword(String newPwd) {
        this.password = newPwd;
    }
    
    public boolean verifyPassword(String pwd) {
        boolean result = this.password.equals(pwd);
        return result;
    }
    
    public boolean verifyCardNumber(String cardNo){
        boolean result = this.cardNumber.equals(cardNo);
        return result;
    }
    
    public boolean isAdministrator(){
        String admin = "Administrator";
        boolean result = this.customerName.equals(admin);
        return result;
    }
}